<?php
	require_once('../admin_bds/config.php');
	require_once('../admin_bds/common_start.php');
	require_once('../admin_bds/lib/func.lib.php');
	$fnc = $_GET['fnc'] ? killInjection($_GET['fnc']) : killInjection($_POST['fnc']);
	switch ($fnc){	
		case 'access_statistics': access_statistics();      break;
	}
	function access_statistics(){
		$time 		= time();
		$today 		= strtotime('today');
		$thismonth 	= strtotime(date('Y-m-01'));
		$thisyear 	= strtotime(date('Y-01-01'));
		$session_id 		= session_id();
		$remote_addr 		= $_SERVER['REMOTE_ADDR'];
		$http_referer 		= $_SERVER['HTTP_REFERER'];
		$http_user_agent 	= $_SERVER['HTTP_USER_AGENT'];
		if(!isset($_SESSION['online'])){
			/* insert visiter info */
			$fields_arr = array(
			"session_id"	=> "'$session_id'",
			"activity"      => "$time",
			"ip_address"    => "'{$remote_addr}'",
			"url"           => "'{$http_referer}'",
			"user_agent"	=> "'{$http_user_agent}'");
			$insert_visiter = insert('xteam_visitor',$fields_arr);
			if($insert_visiter > 0){
			    $_SESSION['online']=true;
				/* update total visits */
				$fields_arr = array(
					'total_visits'	=> 'total_visits+1',
					'date_added'    => $time);
				$update_total = update('xteam_total_visits',$fields_arr,"code='total'");
				if(!$update_total){
					echo json_encode($fields_arr);
				}
				/* get current day */
				$visits_today = selectOne('xteam_total_visits',"code='today' AND date_added = " . $today);
				if($visits_today){
					/* update total visits by day */
					$fields_arr = array('total_visits'	=> $visits_today['total_visits'] + 1);
					update('xteam_total_visits',$fields_arr,"code='today' AND date_added = " . $today);
				}else{
					/* insert total visits by day */
					$fields_arr = array(
						"code"	=> "'today'",
						"name"	=> "'Tổng truy cập ngày " . date('d/m/Y') . "'",
						"total_visits"	=> 1,
						"date_added"   	=> $today);
					$insert_today = insert('xteam_total_visits',$fields_arr);
					if(!$insert_today){
						echo json_encode($fields_arr);
					}
				}
				/* get current month */
				$visits_thismonth = selectOne('xteam_total_visits',"code='thismonth' AND date_added = " . $thismonth);
				if($visits_thismonth){
					/* update total visits by month */
					$fields_arr = array('total_visits'	=> $visits_thismonth['total_visits'] + 1);
					update('xteam_total_visits',$fields_arr,"code='thismonth' AND date_added = " . $thismonth);
				}else{
					/* insert total visits by month */
					$fields_arr = array(
						"code"	=> "'thismonth'",
						"name"	=> "'Tổng truy cập tháng " . date('m/Y') . "'",
						"total_visits"	=> 1,
						"date_added"   	=> $thismonth);
					insert('xteam_total_visits',$fields_arr);
				}
				/* get current year */
				$visits_thisyear = selectOne('xteam_total_visits',"code='thisyear' AND date_added = " . $thisyear);
				if($visits_thisyear){
					/* update total visits by year */
					$fields_arr = array('total_visits'	=> $visits_thisyear['total_visits'] + 1);
					update('xteam_total_visits',$fields_arr,"code='thisyear' AND date_added = " . $thisyear);
				}else{
					/* insert total visits by year */
					$fields_arr = array(
						"code"	=> "'thisyear'",
						"name"	=> "'Tổng truy cập năm " . date('Y') . "'",
						"total_visits"	=> 1,
						"date_added"   	=> $thisyear);
					insert('xteam_total_visits',$fields_arr);
				}
			}else{
//				echo json_encode(array('error' => 'FAIL', 'msg' => 'Can not insert!'));
			}
		} else { 
			$update = false;
		    if(isset($_SESSION['member'])){
		    	$fields_arr = array(
					'activity'	=> $time,
					'member'	=> 'y');
		    	$update = update('xteam_visitor',$fields_arr,"session_id = '" . $session_id . "'");
		    }else{
				$fields_arr = array(
					'activity'	=> $time,
					'member'	=> 'n');
		    	$update = update('xteam_visitor',$fields_arr,"session_id = '" . $session_id . "'");
		    } 
		    /*if(!$update){
		    	die(json_encode(array('error' => 'FAIL', 'msg' => 'Can not update!', 'test' => $session_id)));
		    }*/
		}	
		$maxtime = system_config::visitorTimeout; // 5 Minute time out. 60 * 5 = 300
		$delete_online = delete_where('xteam_visitor','activity < ' . ($time - $maxtime));
		if($delete_online){
			unset($_SESSION['online']);
		}
		$guest  = countRecord('xteam_visitor',"member='n'");
		$members = countRecord('xteam_visitor',"member='y'");
		
		$get_total_visits = selectOne('xteam_total_visits',"code = 'total'");
		$total_visits = $get_total_visits['total_visits'];
		$get_visits_today = selectOne('xteam_total_visits',"code = 'today' AND date_added = " . $today);
		$visits_today = $get_visits_today['total_visits'];
		if($total_visits && $visits_today){
			echo json_encode(
				array(
					'error' 	=> 'OK',
					'online' 	=> _ONLINE . ':',
					//'member_num' 	=> $members,
					//'member' 	=> _MEMBER,
					'guests_num' 	=> FormatNumber($guest),
					//'guests' 	=> _GUEST,
					'access' 	=> _ACCESS . ':',
					'total_visits' 	=> FormatNumber($total_visits),
					'access_today'	=> _ACCESS_TODAY . ':',
					'visits_today' 	=> FormatNumber($visits_today)
				)
			);
		}else{
			echo json_encode(array('error' => 'FAIL', 'msg' => 'Data not found'));
		}
	}
?>



